import React, { Component } from 'react'

import { Link } from 'react-router-dom'
import { Grid, Button, Toast, Modal } from 'antd-mobile'

import {
  REACT_APP_URL as BASE_URL,
  API,
  getToken,
  removeToken,
  isLogin
} from '../../utils'

import styles from './index.module.css'

// 菜单数据
const menus = [
  { id: 1, name: '我的收藏', iconfont: 'icon-coll', to: '/favorate' },
  { id: 2, name: '我的出租', iconfont: 'icon-ind', to: '/rent' },
  { id: 3, name: '看房记录', iconfont: 'icon-record' },
  {
    id: 4,
    name: '成为房主',
    iconfont: 'icon-identity'
  },
  { id: 5, name: '个人资料', iconfont: 'icon-myinfo' },
  { id: 6, name: '联系我们', iconfont: 'icon-cust' }
]

// 默认头像
const DEFAULT_AVATAR = BASE_URL + '/img/profile/avatar.png'

export default class Profile extends Component {
  constructor(props) {
    super(props)
    console.log('cccccccccc', isLogin, ' fff', this.state.isLogin)
  }
  state = {
    userInfo: '',
    isLogin: isLogin()
  }
  render() {
    const { history } = this.props
    const { avatar, nickname } = this.state.userInfo
    const { isLogin } = this.state
    return (
      <div className={styles.root}>
        {/* 个人信息 */}
        <div className={styles.title}>
          <img
            className={styles.bg}
            src={BASE_URL + '/img/profile/bg.png'}
            alt="背景图"
          />
          <div className={styles.info}>
            <div className={styles.myIcon}>
              <img
                className={styles.avatar}
                src={avatar ? BASE_URL + avatar : DEFAULT_AVATAR}
                alt="icon"
              />
            </div>
            <div className={styles.user}>
              <div className={styles.name}>{nickname || '游客'}</div>
              {/* 登录后展示： */}
              {isLogin && (
                <>
                  <div className={styles.auth}>
                    <span onClick={this.logout}>退出</span>
                  </div>
                  <div className={styles.edit}>
                    编辑个人资料
                    <span className={styles.arrow}>
                      <i className="iconfont icon-arrow" />
                    </span>
                  </div>
                </>
              )}

              {/* 未登录展示： */}
              {!isLogin && (
                <div className={styles.edit}>
                  <Button
                    type="primary"
                    size="small"
                    inline
                    onClick={() => history.push('/login')}
                  >
                    去登录
                  </Button>
                </div>
              )}
            </div>
          </div>
        </div>

        {/* 九宫格菜单 */}
        <Grid
          data={menus}
          columnNum={3}
          hasLine={false}
          renderItem={item =>
            item.to ? (
              <Link to={item.to}>
                <div className={styles.menuItem}>
                  <i className={`iconfont ${item.iconfont}`} />
                  <span>{item.name}</span>
                </div>
              </Link>
            ) : (
              <div className={styles.menuItem}>
                <i className={`iconfont ${item.iconfont}`} />
                <span>{item.name}</span>
              </div>
            )
          }
        />

        {/* 加入我们 */}
        <div className={styles.ad}>
          <img src={BASE_URL + '/img/profile/join.png'} alt="" />
        </div>
      </div>
    )
  }

  componentDidMount() {
    this.getUserInfo()
  }
  // 获取用户信息
  async getUserInfo() {
    const { isLogin } = this.state

    // 未登录
    if (!isLogin) {
      return
    }

    // 登录了
    let res = await API.get('/user', {
      headers: {
        authorization: getToken()
      }
    })
    console.log('res', res)
    const { status } = res.data

    if (status === 200) {
      this.setState({
        userInfo: res.data.body
      })
    } else if (status === 400) {
      removeToken()
      this.setState({
        isLogin: false
      })
    } else {
      Toast.info('获取用户信息失败，请重新登录', 2)
    }
  }
  // 点击退出
  logout = () => {
    const alert = Modal.alert
    alert('温馨提示', '您确定要退出吗?', [
      {
        text: '取消',
        onPress: () => console.log('cancel'),
        style: 'default'
      },
      {
        text: '确定',
        onPress: async () => {
          const result = await API.post('/user/logout', null, {
            headers: {
              authorization: getToken()
            }
          })
          if (result.data.status === 200) {
            removeToken()
            this.setState({
              userInfo: '',
              isLogin: false
            })
          } else {
            Toast.fail('退出失败', 2)
          }
        }
      }
    ])
  }
}
